<template>
  <div class="search-wrapper">
    <!-- 搜索框 -->
    <search-box
      ref="searchBox"
      @query="handleQueryChange"
      ></search-box>
    <div class="shortcut-wrapper" v-show="!query">
      <scroll class="scroll" ref="shortcut" :data="shortcut">
        <div>
          <!-- 热门搜索区 -->
          <div class="shortcut">
            <h1 class="title">热门搜索</h1>
            <ul class="hotkey-list" v-if="hotkey.length > 0">
              <li
                class="hotkey special-key"
                @click="handleHotkeyClick(specialKey)"
                >
                {{specialKey}}
              </li>
              <li
                class="hotkey"
                v-for="item in computedHotkey"
                :key="item.n"
                @click="handleHotkeyClick(item.k)"
                >
                {{item.k}}
              </li>
            </ul>
          </div>
          <!-- 搜索历史区 -->
          <div class="shortcut" v-show="searchHistory.length">
            <h1 class="title">
              <span>搜索历史</span>
              <i class="icon-clear" @click="clearHistory"></i>
            </h1>
            <search-list
              :list="searchHistory"
              @select="handleHotkeyClick"
              @delete="deleteSearchHistory"
              ></search-list>
          </div>
        </div>
      </scroll>
    </div>
    <!-- 搜索结果列表 -->
    <suggest
      v-show="query"
      :query="query"
      @listScroll="blurInput"
      @select="saveSearchHistory(query)"
      ></suggest>
    <!-- 路由 -->
    <slide>
      <router-view></router-view>
    </slide>
    <!-- 弹层 -->
    <confirm
      ref="confirm"
      text="是否清空所有搜索历史？"
      okBtn="清空"
      @ok="clearSearchHistory"
      ></confirm>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { getHotKey } from 'api'
import SearchBox from 'base/search-box/search-box'
import Slide from 'base/slide/slide'
import Suggest from 'components/suggest/suggest'
import SearchList from 'base/search-list/search-list'
import Confirm from 'base/confirm/confirm'
import Scroll from 'base/scroll/scroll'
import { playlistMixin, searchMixin } from 'common/js/mixin'

export default {
  name: 'search',
  mixins: [playlistMixin, searchMixin],
  components: {
    SearchBox,
    Suggest,
    Slide,
    SearchList,
    Confirm,
    Scroll
  },
  data () {
    return {
      specialKey: '',
      hotkey: []
    }
  },
  created () {
    this._getHotKey()
  },
  computed: {
    computedHotkey () {
      const ret = []
      this.hotkey.forEach((item, index) => {
        if (index <= 8) {
          ret.push(item)
        }
      })
      return ret
    },
    shortcut () {
      return this.computedHotkey.concat(this.searchHistory)
    }
  },
  methods: {
    handleHotkeyClick (query) {
      this.$refs.searchBox.addQuery(query)
    },
    clearHistory () {
      this.$refs.confirm.show()
    },
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''

      this.$refs.shortcut.$el.style.bottom = bottom
      this.$refs.shortcut.refresh()
    },
    _getHotKey () {
      getHotKey().then((data) => {
        // console.log(data)
        this.specialKey = data.special_key
        this.hotkey = data.hotkey
      })
    },
    ...mapActions([
      'clearSearchHistory'
    ])
  },
  watch: {
    query (newQuery) {
      if (!newQuery) {
        setTimeout(() => {
          this.$refs.shortcut.refresh()
        }, 20)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~common/stylus/variable.styl'
  .search-wrapper {
    padding: 20px
    .shortcut-wrapper {
      .shortcut {
        padding: 20px 20px 0 20px
        &:last-child {
          padding-bottom: 20px
        }
        .title {
          font-size: $font-size-medium
          color: $color-text-l
          .icon-clear {
            float: right
            color: $color-text-d
            font-size: $font-size-medium-x
          }
        }
        .hotkey-list {
          overflow: hidden
          margin-top: 16px
          .hotkey {
            float: left
            padding: 4px 8px
            background: $color-highlight-background
            margin-right: 16px
            font-size: $font-size-small
            color: $color-text-d
            border-radius: 4px
            margin-bottom: 12px
            box-sizing: border-box
            border: 1px solid $color-highlight-background
            &.special-key {
              color: $color-theme
              border: 1px solid $color-theme
              box-sizing: border-box
            }
          }
        }
      }
      .scroll {
        position: absolute
        top: 149px
        right: 0
        bottom: 0
        left: 0
        // background: red
        overflow: hidden
      }
    }
  }
</style>
